package com.corelib;

import android.app.Activity;
import android.app.Application;
import android.os.Build;
import android.os.Bundle;

import com.corelib.config.AppConfig;
import com.corelib.utils.CommonUtils;
import com.corelib.utils.LogUtils;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.orhanobut.logger.Logger;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.log.LoggerInterceptor;

import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

/**
 * Created by lenovo on 2016/12/27.
 */
public class BaseApplication extends Application {
    private static BaseApplication application;
    public LifecycleCallbacks callbacks;
    @Override
    public void onCreate() {
        super.onCreate();
        if(!CommonUtils.getCurProcessName(this).equals(getPackageName())){
            return;
        }
        application = this;
        LogUtils.logInit();
        Fresco.initialize(this);
        OkHttpUtils.initClient(getOKHttp());
        registerActivityLiftCycleLinstener();
    }
    public static OkHttpClient getOKHttp() {
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
        if("dev".equals(AppConfig.getVersionType())){
            builder.addInterceptor(new LoggerInterceptor("net", true));
        }

        return builder.build();
    }
    public static BaseApplication getApplication(){
        return application;
    }
    private void registerActivityLiftCycleLinstener(){
        callbacks = new LifecycleCallbacks();
        registerActivityLifecycleCallbacks(callbacks);
    }

   public class LifecycleCallbacks implements ActivityLifecycleCallbacks{
        public int ACTIVITY_COUNT;
        public Activity currentActivity;
        public boolean isBackground = true;
        @Override
        public void onActivityCreated(Activity activity, Bundle bundle) {
            Logger.t("activity").i(activity.getClass().getSimpleName()+"********onActivityCreated");
            currentActivity = activity;
            ACTIVITY_COUNT++;
            isBackground = false;
        }

        @Override
        public void onActivityStarted(Activity activity) {
            Logger.t("activity").i(activity.getClass().getSimpleName()+"********onActivityStarted");
        }

        @Override
        public void onActivityResumed(Activity activity) {
            Logger.t("activity").i(activity.getClass().getSimpleName()+"********onActivityResumed");
            isBackground = false;
        }

        @Override
        public void onActivityPaused(Activity activity) {
            Logger.t("activity").i(activity.getClass().getSimpleName()+"********onActivityPaused");
        }

        @Override
        public void onActivityStopped(Activity activity) {
            Logger.t("activity").i(activity.getClass().getSimpleName()+"********onActivityStopped");
            isBackground = true;
        }

        @Override
        public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

        }

        @Override
        public void onActivityDestroyed(Activity activity) {
            Logger.t("activity").i(activity.getClass().getSimpleName()+"********onActivityDestroyed");
            ACTIVITY_COUNT--;
        }
    }
}
